<template>
  <a-modal :title="title" :width="width" :visible="visible" :okButtonProps="{ class: { 'jee-hidden': disableSubmit } }" @cancel="handleCancel">
    <template #footer>
      <a-button key="back" type="danger" @click="handleReject">{{ cancelTxt }}</a-button>
      <a-button v-if="!disableSubmit" key="submit" type="primary" @click="handleOk">审核通过</a-button>
    </template>

    <CamelliaMesProcessPlanForm ref="registerForm" @ok="submitCallback" :formDisabled="disableSubmit" :formBpm="false"></CamelliaMesProcessPlanForm>
  </a-modal>
</template>

<script lang="ts" setup>
import responsiveObserve from 'ant-design-vue/lib/_util/responsiveObserve';
import { ref, nextTick, defineExpose } from 'vue';
import CamelliaMesProcessPlanForm from './CamelliaMesProcessPlanForm.vue';
import { defHttp } from '/@/utils/http/axios';

const title = ref<string>('');
const width = ref<number>(800);
const visible = ref<boolean>(false);
const disableSubmit = ref<boolean>(false);
const registerForm = ref();
const emit = defineEmits(['register', 'success', 'ok']);
const cancelTxt = ref<string>('');
let formData;

/**
 * 编辑
 * @param record
 */
function edit(record) {
  //暂存表单信息，供“退回”用
  formData = record;

  title.value = disableSubmit.value ? '详情' : '审核';
  cancelTxt.value = disableSubmit.value ? '关闭' : '退回';
  visible.value = true;
  let params = { orderId: record.id };
  let url = '/mes/camelliaMesProcessPlan/all_enable';

  defHttp.get({ url, params }).then((respose) => {
    let machines = [];
    for (let item of respose) {
      let machine = { value: item.id, label: item.deviceName };
      machines.push(machine);
    }

    record.machinesOptions = machines;

    nextTick(() => {
      registerForm.value.edit(record);
    });
  });
}

/**
 * 确定按钮点击事件
 */
function handleOk() {
  registerForm.value.submitForm();
}

/**
 * form保存回调事件
 */
function submitCallback() {
  handleCancel();
  emit('success');
}

/**
 * 取消按钮回调事件
 */
function handleCancel() {
  visible.value = false;
}

/**
 * 退回按钮事件
 */
function handleReject() {
  if (!disableSubmit.value) {
    console.log('reject-----');
    //在这里调用接口
    let url = '/mes/myplan/back';
    delete registerForm.value.formData.machinesOptions;
    let params = registerForm.value.formData;
    defHttp.post({ url, params }).then((res) => {
      console.log(res);
      emit('ok');
      visible.value = false;
    });
  }

  visible.value = false;
}

defineExpose({
  edit,
  disableSubmit,
});
</script>

<style>
/**隐藏样式-modal确定按钮 */
.jee-hidden {
  display: none !important;
}
</style>
